{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7615972a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679282240,e7a4a9a4-8cf6-45f4-9a67-84e1d66daed0', 'time_used': 17, 'error_message': 'MISSING_ARGUMENTS: face_token1, image_base64_1, image_url1, image_file1'}\n",
      "400\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸检测\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f_01 = open('2.jpg', 'rb')\n",
    "f_02 = open('1.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "params = {\"api_key\":\"CXQao5z3g20cbIGZu_0DvivvKCrW_sHy\",\n",
    "          \"api_secret\":\"5e72pKCrd177J9IUFwkb6wfC_701HTkT\",\n",
    "          \"image_base64_1 \": img_01,\n",
    "          \"image_base64_2 \": img_02,\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9957e242",
   "metadata": {},
   "source": [
    "## 人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aef65eb5",
   "metadata": {},
   "source": [
    "### 创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4f298fbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': 'ae82ff3add4dd75b323dcd831bb34d41', 'time_used': 153, 'face_count': 0, 'face_added': 0, 'request_id': '1679283060,dad4f934-92c2-4634-8bd4-37544611f6fc', 'outer_id': '', 'failure_detail': []}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\"api_key\":\"CXQao5z3g20cbIGZu_0DvivvKCrW_sHy\",\n",
    "          \"api_secret\":\"5e72pKCrd177J9IUFwkb6wfC_701HTkT\",\n",
    "          \"display_name\" : \"广州南方学院1栋教学楼\"\n",
    "   \n",
    "}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43e0a964",
   "metadata": {},
   "source": [
    "### 检查人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b6753150",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '434e3c7cf8227e763cb4370a9e6961c0',\n",
       " 'time_used': 190,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1679284489,2c4fd6fb-5c36-4185-b9ff-5c958cedefbd',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "'''\n",
    "人脸数据集合详情查询\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\"api_key\":\"CXQao5z3g20cbIGZu_0DvivvKCrW_sHy\",\n",
    "          \"api_secret\":\"5e72pKCrd177J9IUFwkb6wfC_701HTkT\",\n",
    "          \"faceset_token\": \"ae82ff3add4dd75b323dcd831bb34d41\"\n",
    "       \n",
    "   \n",
    "}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "\n",
    "print(response.status_code)\n",
    "(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79e12d05",
   "metadata": {},
   "source": [
    "### 增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "4178177d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679284479,5c6389d5-114c-4563-badc-66653c5d4b81', 'time_used': 101, 'faces': [{'face_token': '453e225f178c129e070e837ed8f131a2', 'face_rectangle': {'top': 97, 'left': 60, 'width': 115, 'height': 115}}], 'image_id': 'PS1Nlmkn1snmbysZlpZ+mg==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "#### 先检测人脸信息\n",
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测：邓紫棋、蔡徐坤、刘亦菲\n",
    "'e6e96241186642380a52eee7849f81b6','35962498e1ecc6daded3c3bf984bbe86','91a54df9ce308a58cc074430f6f67932'\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('1栋/lyf.jfif', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload={\n",
    "    'api_key':'CXQao5z3g20cbIGZu_0DvivvKCrW_sHy',\n",
    "    'api_secret':'5e72pKCrd177J9IUFwkb6wfC_701HTkT',\n",
    "    'image_base64':img,\n",
    "#     'return_attributes':'gender,age,smiling,headpos,facequality,emotion,beauty,eyegaze'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "900da684",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ae82ff3add4dd75b323dcd831bb34d41',\n",
       " 'time_used': 616,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1679284230,d7782644-f2db-44be-9574-a2c32e7362f9',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\"api_key\":\"CXQao5z3g20cbIGZu_0DvivvKCrW_sHy\",\n",
    "          \"api_secret\":\"5e72pKCrd177J9IUFwkb6wfC_701HTkT\",\n",
    "          \"faceset_token\": \"ae82ff3add4dd75b323dcd831bb34d41\",\n",
    "          \"face_tokens\":['e6e96241186642380a52eee7849f81b6','35962498e1ecc6daded3c3bf984bbe86','91a54df9ce308a58cc074430f6f67932']\n",
    "       \n",
    "   \n",
    "}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "\n",
    "print(response.status_code)\n",
    "(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95fdc35c",
   "metadata": {},
   "source": [
    "### 检查人脸集合的人脸信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "451a8106",
   "metadata": {},
   "source": [
    "### 人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbab5b94",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
